/**
 * Project Name:scb_sms
 * File Name:MenuServiceImpl.java
 * Package Name:com.scb.dev.sms.service.sm.impl
 * Date:Nov 20, 201812:50:48 PM
 * Copyright (c) 2018, Y-Rainson All Rights Reserved.
 *
 */
package com.scb.dev.mms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.MenuMapper;
import com.scb.dev.mms.dao.RmoGrantMapper;
import com.scb.dev.mms.dao.RoleMapper;
import com.scb.dev.mms.pojo.Menu;
import com.scb.dev.mms.service.IMenuService;
import com.scb.dev.mms.service.IRmoGrantService;
import com.scb.dev.mms.util.UUIDTool;
import com.scb.dev.mms.util.tree.TreeNode;
import com.scb.dev.mms.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * ClassName: MenuServiceImpl <br/>
 * Description: date: Nov 20, 2018 12:50:48 PM <br/>
 *
 * @author Y-Rainson
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Service
public class MenuServiceImpl implements IMenuService {

	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private RmoGrantMapper rmoGrantMapper;


	/**
	 *
	 * @see IMenuService#addMenu(Menu)
	 */
	@Override
	public ResponseResult addMenu(Menu menu) {
		if (menu == null) {
			return ResponseResult.resultError(ResultType.ERROR);
		} else {
			menu.setMenuId(UUIDTool.generateUUID());
			int flag = menuMapper.insertSelective(menu);
			if (flag == StatusCode.FAULT_FLAG) {
				return ResponseResult.resultError(ResultType.ADD_ERROR);
			} else {
				return ResponseResult.resultSuccess(menu, ResultType.ADD_SUCCESS);
			}
		}
	}

	/**
	 * 
	 * @see IMenuService#deleteMenu(String)
	 */
	@Override
	public ResponseResult deleteMenu(String menuId) {
		if (menuId != null) {
			int flag = menuMapper.deleteByMenuId(menuId);
			if (flag == StatusCode.FAULT_FLAG) {
				return ResponseResult.resultError(ResultType.DELETE_ERROR);
			} else {
				return ResponseResult.resultSuccess(menuId, ResultType.DELETE_SUCCESS);
			}
		} else {
			return ResponseResult.resultError(ResultType.FIND_NULL);
		}

	}

	/**
	 * 
	 * 
	 * @see IMenuService#queryAllMenu()
	 */
	@Override
	public ResponseResult queryAllMenu() {
		List<Menu> menuList = menuMapper.selectAllMenu();
		if (menuList.size() == 0) {
			return ResponseResult.resultSuccess(null, ResultType.FIND_NULL);
		}
		return ResponseResult.resultSuccess(menuList, ResultType.OPERATION_SUCCESS);
	}

	/**
	 * 
	 * 
	 * @see IMenuService#updateMenu(Menu)
	 */
	@Override
	public ResponseResult updateMenu(Menu menu) {
		if (menu == null) {
			return ResponseResult.resultError(ResultType.FIND_NULL);
		} else {
			int flag = menuMapper.updateByMenuIdSelective(menu);
			if (flag == StatusCode.FAULT_FLAG) {
				return ResponseResult.resultError(ResultType.UPDATE_ERROR);
			} else {
				return ResponseResult.resultSuccess(menu, ResultType.UPDATE_SUCCESS);
			}
		}

	}

	/**
	 * 
	 * @see IMenuService#generateMenuTree()
	 */
	@Override
	public ResponseResult generateMenuTree() {
		List<TreeNode> treeNodeList = new ArrayList<>();
		List<Menu> menuList = menuMapper.selectAllMenu();
		List menuTree = new ArrayList<>();
		if (menuList.size() == 0) {
			return ResponseResult.resultError(ResultType.FIND_NULL);
		} else {
			for (Menu menu : menuList) {
				treeNodeList.add(new TreeNode(menu.getMenuId(), menu.getMenuParentId(),menu.getMenuName(),menu.getMenuCreatorUrl()));
			}
			if (treeNodeList.size() == 0) {
				return ResponseResult.resultError(ResultType.ERROR);
			} else {
				TreeUtil treeUtil = new TreeUtil();
				menuTree = treeUtil.treeMenu(treeNodeList);
				if (menuTree.size() == 0) {
					return ResponseResult.resultError(ResultType.ERROR);
				} else {
					return ResponseResult.resultSuccess(menuTree, ResultType.OPERATION_SUCCESS);
				}

			}
		}

	}

	/**
	 *
	 * @see IMenuService#generateMenuTree(String roleId)
	 */
	@Override
	public ResponseResult generateMenuTree(String roleId) {
		List<String> menuIdList = rmoGrantMapper.selectMenuIdByRoleId(roleId);
		List<Menu> menuList = new ArrayList<>(menuIdList.size());
		for (String s : menuIdList) {
			menuList.add(menuMapper.selectByMenuId(s));
		}
		List menuTree = new ArrayList<>();
		List<TreeNode> treeNodeList = new ArrayList<>();
		if (menuList.size() == 0) {
			return ResponseResult.resultError(ResultType.FIND_NULL);
		} else {
			for (Menu menu : menuList) {
				treeNodeList.add(new TreeNode(menu.getMenuId(), menu.getMenuParentId(),menu.getMenuName(),menu.getMenuCreatorUrl()));
			}
			if (treeNodeList.size() == 0) {
				return ResponseResult.resultError(ResultType.ERROR);
			} else {
				TreeUtil treeUtil = new TreeUtil();
				menuTree = treeUtil.treeMenu(treeNodeList);
				if (menuTree.size() == 0) {
					return ResponseResult.resultError(ResultType.ERROR);
				} else {
					return ResponseResult.resultSuccess(menuTree, ResultType.OPERATION_SUCCESS);
				}

			}
		}
	}

}
